/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelLogin.java
 *
 * Created on Dec 3, 2010, 10:39:16 PM
 */
package stta.elinfo.perpustakaan.gui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import stta.elinfo.perpustakaan.dao.entity.User;
import stta.elinfo.perpustakaan.dao.impl.UserImpl;
import stta.elinfo.perpustakaan.dao.interf.UserInterf;

/**
 *
 * @author fendi
 */
public class PanelLogin extends javax.swing.JPanel {

    private Paint paint;
    private Color color;
    UserInterf user;

    /** Creates new form PanelLogin */
    public PanelLogin() {
        initComponents();
        // setOpaque(false);
        setSize(600, 400);
        user = new UserImpl();
        this.status();
        color = new Color(getBackground().getRed(), getBackground().getGreen(), getBackground().getRed(), 125);

        addMouseListener(new MouseAdapter() {
        });
        addMouseMotionListener(new MouseAdapter() {
        });
        addMouseWheelListener(new MouseAdapter() {
        });
        addKeyListener(new KeyAdapter() {
        });
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        color = new Color(getBackground().getRed(), getBackground().getGreen(), getBackground().getRed(), 125);
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D gd = (Graphics2D) g.create();
        // color=new Color(getBackground().getRed(),get , TOP_ALIGNMENT, TOP_ALIGNMENT)
        color = new Color(getBackground().getRed(), getBackground().getGreen(), getBackground().getRed(), 125);
        paint = new GradientPaint(0, 0, new Color(1F, 1F, 1F, 0.5F), getWidth(), 0, new Color(1F, 1F, 1F, 0.5F));
        gd.setBackground(color);
        // gd.setPaint(paint);
        gd.fillRect(0, 0, getWidth(), getHeight());
        gd.dispose();
        super.paintComponent(g);
    }

    void status() {
        try {
            String nama = this.user.getUser().getNama();
            if (nama == null) {
                btnLogin.setText("Create User");
            } else {
                txtName.setText("");
                txtPass.setText("");
                btnLogin.setText("Login");
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    boolean cekLogin(String name, String pass) {
        try {
            if (this.user.getUser().getNama().equals(name) && this.user.getUser().getPassword().equals(pass)) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelLogin.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtName = new stta.elinfo.perpustakaan.template.TextField();
        btnLogin = new stta.elinfo.perpustakaan.template.Button();
        buttonTransparan1 = new stta.elinfo.perpustakaan.template.ButtonTransparan();
        buttonTransparan11 = new stta.elinfo.perpustakaan.template.ButtonTransparan1();
        txtPass = new stta.elinfo.perpustakaan.template.PasswordTextField();

        setBackground(new java.awt.Color(153, 0, 153));
        setOpaque(false);

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 18));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("USER LOGIN");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Username");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Password");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        buttonTransparan1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stta/elinfo/perpustakaan/gambar/LOGIN.png"))); // NOI18N

        buttonTransparan11.setForeground(new java.awt.Color(0, 102, 255));
        buttonTransparan11.setText("Perpustakaan Mini");
        buttonTransparan11.setFont(new java.awt.Font("Lucida Sans", 1, 36));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonTransparan11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(88, 88, 88)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                                            .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)))
                                    .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18))
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE))
                        .addComponent(buttonTransparan1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(47, 47, 47))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(buttonTransparan11, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonTransparan1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(64, 64, 64))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
        if (btnLogin.getText().equals("Create User")) {
            try {
                User us = new User();
                us.setNama(txtName.getText());
                us.setPassword(txtPass.getText());
                this.user.insert(us);
                txtName.setText("");
                txtPass.setText("");
                txtName.requestFocus();
                btnLogin.setText("Login");
                JOptionPane.showMessageDialog(this, "Data User anda Sudah tersimpan, Selamat menikmati Fasilitas Perpustakaan Mini");
            } catch (SQLException ex) {
                Logger.getLogger(PanelLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            if (cekLogin(txtName.getText(), txtPass.getText())) {
                this.setVisible(false);

            } else {
                JOptionPane.showMessageDialog(this, "Username dan password tidak sesuai");
            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private stta.elinfo.perpustakaan.template.Button btnLogin;
    private stta.elinfo.perpustakaan.template.ButtonTransparan buttonTransparan1;
    private stta.elinfo.perpustakaan.template.ButtonTransparan1 buttonTransparan11;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private stta.elinfo.perpustakaan.template.TextField txtName;
    private stta.elinfo.perpustakaan.template.PasswordTextField txtPass;
    // End of variables declaration//GEN-END:variables
}
